home *** CD-ROM | disk | FTP | other *** search
- /* jove_main.c */
-
- /*
- Jonathan Payne at Lincoln-Sudbury Regional High School 4-19-83
-
- jove_main.c
-
- Contains the main loop, initializations, getch routine... */
-
-
- #include "jove.h"
-
- #ifdef UNIX
- #include <sys/ioctl.h>
- #include <signal.h>
- #include <sgtty.h>
- #else
- #include "signal.h"
- #endif
-
- #ifdef BUGS
- extern char *stdout;
- #endif
-
-
- extern int EscPrefix();
- extern int CtlxPrefix();
- extern int Forget();
- extern int errormsg;
- extern int OKXonXoff; /* funcs.h */
- extern FMACRO *macstack[NMACROS]; /* funcs.h */
- extern FMACRO KeyMacro; /* funcs.h */
- extern int stackp; /* funcs.h */
- extern char *tfname;
-
-
- #define NTOBUF 20 /* Should never get that far ahead */
-
- /* globals */
-
-
- int (*Gtchar)();
-
- int
- tabstop,
- LastKeyStruck,
- UpdMesg,
- BufSize,
- peekc,
- io, /* file desc. */
- exp,
- exp_p,
- this_cmd,
- last_cmd,
- Input, /* char that is waiting */
- InputPending, /* 0 if no char waiting */
- killptr,
- CanScroll,
- Asking, /* 1 if inputting string */
- globflags[NFLAGS];
-
- char
- **argvp,
- mesgbuf[100],
- linebuf[LBSIZE],
- genbuf[LBSIZE];
-
- LINE
- *killbuf[NUMKILLS]; /* Array of pointers to killed stuff */
-
- WINDOW
- *fwind, /* first window in list */
- *curwind; /* current window */
-
-
- BUFFER
- *curbuf; /* Pointer into world for current buffer */
-
- FUNCT
- *mainmap[0200],
- *pref1map[0200],
- *pref2map[0200],
- *LastFunc;
-
- #ifndef UNIX
- FUNC eprefix;
- FUNC cprefix;
- FUNC frgt;
- #endif
-
- #ifdef UNIX
- extern IOBUF
- termout;
- #endif
-
- /* o.s. dependent var. */
-
- #ifdef UNIX
- #ifdef TIOCSLTC
- struct ltchars ls1,
- ls2;
- #endif
- struct tchars tc1,
- tc2;
- #endif
-
- /* non-global but shared */
-
- int errormsg, Crashing;
- int origflags[NFLAGS];
- char *Mainbuf = "Main";
- BUFFER *world; /* First buffer */
- jmp_buf mainjmp;
-
- /* static var */
-
- static char smbuf[NTOBUF],
- *bp = smbuf;
- static int nchars = 0;
-
- static int iniargc;
- static char **iniargv;
-
-
-
- finish(code)
- {
- char c;
- int Crashit = code && (code != LOGOEXIT);
-
- if (code == SIGINT) {
-
-
- #ifdef UNIX
- #ifndef SIGTSTP /* Job stopping in other words */
- ignorf(signal(code, finish));
- #endif
- #endif
- message("Quit? ");
- UpdateMesg();
- ignore(read(0, &c, 1));
- message("");
- if ((c & 0377) != 'y') {
- redisplay();
- return;
- }
- }
- if (Crashit) {
- if (!Crashing) {
- putstr("Writing modified JOVE buffers...");
- Crashing++;
- exp_p = 0;
- WtModBuf();
- } else
- putstr("Complete lossage!");
- }
- ttyset(0);
- #ifdef UNIX
- term_exit();
- #else
- closetmp();
- #endif
- ignore(unlink(tfname));
- if (Crashit)
- abort();
- #ifndef UNIX
- cleanup();
- exit(0);
- #else
- exit(code);
- #endif
- }
-
-
- Ungetc(c)
- int c;
- {
- if (c == EOF || nchars >= NTOBUF)
- return EOF;
- *--bp = c;
- nchars++;
- return c;
- }
-
- getchar()
- {
- if (nchars == 0) {
- #ifdef UNIX
- if ((nchars = read(Input, smbuf, sizeof smbuf)) == 0) {
- #else
- if ((nchars = conread(Input, smbuf, sizeof smbuf)) == 0) {
- #endif
- if (Input)
- return EOF;
- finish(0);
- }
- bp = smbuf;
- InputPending = nchars > 1;
- }
- nchars--;
- return *bp++;
- }
-
- PauseJove()
- {
- #ifdef UNIX
- UnsetTerm();
- #endif
-
- #ifdef SIGTSTP
- ignore(kill(0, SIGTSTP));
- #else
- Suspend();
- #endif
- #ifdef UNIX
- ResetTerm();
- #endif
- ClAndRedraw();
- }
-
-
-
-
- ReInitTTY()
- {
- ttyset(0); /* Back to original settings */
- ttinit();
- }
-
-
- /* NOSTRICT */
-
- char *
- emalloc(size)
- {
- char *ptr;
-
- if (ptr = malloc((unsigned) size)) {
-
- #ifndef UNIX
- setmem(ptr,(unsigned) size,0);
- #endif
- return ptr;
- }
- GCchunks();
- if (ptr = malloc((unsigned) size)) {
- #ifndef UNIX
- setmem(ptr,(unsigned) size,0);
- #endif
- return ptr;
- }
- error("out of memory");
- /* NOTREACHED */
- }
-
- dispatch(c)
- register int c;
- {
- FUNCT *fp;
-
- fp = mainmap[c];
- if (fp == 0) {
- rbell();
- exp = 1;
- exp_p = errormsg = 0;
- message("");
- return;
- }
- ExecFunc(fp, 0);
- }
-
- getch()
- {
- int c;
-
- if (stackp >= 0 && macstack[stackp]->Flags & EXECUTE)
- c = MacGetc();
- else {
- redisplay();
- if ((c = getchar()) == EOF)
- finish(SIGHUP);
- c &= 0177;
- if (KeyMacro.Flags & DEFINE)
- MacPutc(c);
- }
- LastKeyStruck = c;
- return c;
- }
-
- parse(argc, argv)
- char *argv[];
- {
- BUFFER *firstbuf = 0;
- char c;
-
- message("Jonathan's Own Version of Emacs");
-
- *argv = (char *) 0;
- argvp = argv + 1;
-
- while (argc > 1) {
- if (argv[1][0] == '-') {
- if (argv[1][1] == 't') {
- ++argv;
- --argc;
- exp_p = 1;
- find_tag(argv[1]);
- if (!firstbuf)
- firstbuf = curbuf;
- }
- } else if (argv[1][0] == '+' &&
- (c = argv[1][1]) >= '0' && c <= '9') {
- ++argv;
- --argc;
- SetBuf(do_find(curwind, argv[1]));
- if (!firstbuf)
- firstbuf = curbuf;
- SetLine(next_line(curline, atoi(&argv[0][1]) - 1));
- } else {
- SetBuf(do_find(curwind, argv[1]));
- if (!firstbuf)
- firstbuf = curbuf;
- }
-
- ++argv;
- --argc;
- }
-
- if (firstbuf)
- SetBuf(firstbuf);
- }
-
- copy_n(f, t, n)
- register int *f,
- *t,
- n;
- {
- while (n--)
- *f++ = *t++;
- }
-
- #ifdef lint
- Ignore(a)
- char *a;
- {
-
- a = a;
- }
-
- Ignorf(a)
- int (*a)();
- {
-
- a = a;
- }
- #endif
-
- /* VARARGS1 */
-
- error(fmt, args)
- char *fmt;
- {
- if (fmt) {
- format(mesgbuf, fmt, &args);
- UpdMesg++;
- }
- rbell();
- longjmp(mainjmp, ERROR);
- }
-
- /* VARARGS1 */
-
- complain(fmt, args)
- char *fmt;
- {
- if (fmt) {
- format(mesgbuf, fmt, &args);
- UpdMesg++;
- }
- rbell(); /* Always ring the bell now */
- longjmp(mainjmp, COMPLAIN);
- }
-
- /* VARARGS1 */
-
- confirm(fmt, args)
- char *fmt;
- {
- char *yorn;
- char *def;
- def = "\0";
-
- format(mesgbuf, fmt, &args);
- yorn = ask(def, mesgbuf);
- if (*yorn != 'Y' && *yorn != 'y')
- longjmp(mainjmp, COMPLAIN);
- return;
- }
-
- read_ch()
- {
- int c;
- if ((c = peekc) != -1) {
- peekc = -1;
- return c;
- }
- return getch();
- }
-
- DoKeys(first)
- {
- int c;
- jmp_buf savejmp;
- int code;
-
- copynchar((char *) savejmp, (char *) mainjmp, sizeof savejmp);
-
- code = setjmp(mainjmp);
- switch (code) {
- case 0:
- if (first)
- parse(iniargc, iniargv);
- break;
-
- case QUIT:
- copynchar((char *) mainjmp, (char *) savejmp, sizeof mainjmp);
-
- return;
-
- case ERROR:
- getDOT(); /* God knows what state linebuf was in */
-
- case COMPLAIN:
- IOclose();
- Gtchar = getch;
- if (Input) {
- ignore(close(Input));
- Input = 0; /* Terminal has control now */
- }
- errormsg++;
- FixMacros();
- Asking = 0; /* Not anymore we ain't */
- redisplay();
- break;
- }
-
- this_cmd = last_cmd = 0;
-
- for (;;) {
- exp = 1;
- exp_p = 0;
- last_cmd = this_cmd;
- cont:
- this_cmd = 0;
- c = read_ch();
- if(errormsg) {
- message("");
- errormsg = 0;
- redisplay();
- }
-
- if (c == -1)
- continue;
- dispatch(c);
- if (this_cmd == ARG_CMD)
- goto cont;
- }
- }
-
-
-
- main(argc, argv)
- char *argv[];
- {
- extern char searchbuf[];
- char *home;
-
- #ifndef UNIX
- eprefix = &EscPrefix;
- cprefix = &CtlxPrefix;
- frgt = &Forget;
- #endif
-
- tabstop = 4;
- peekc = -1;
- killptr = errormsg = 0;
- curbuf = world = (BUFFER *) 0;
-
- iniargc = argc;
- iniargv = argv;
-
- searchbuf[0] = '\0';
- InputPending = 0;
- Asking = 0;
- Crashing = 0;
- Input = 0; /* Terminal? */
-
- Gtchar = getch;
-
- if (setjmp(mainjmp)) {
- printf("Pre-error: \"%s\"; tell Jon Payne\n", mesgbuf);
- finish(0);
- }
-
- getTERM(0);
- InitCM();
- settout();
- make_scr(); /* Do this before making zero */
- tmpinit(); /* Init temp file */
- MacInit(); /* Initialize Macros */
- InitFuncs(); /* Initialize functions and variables */
- InitBindings(); /* Everyday EMACS commands */
- winit(); /* Initialize window */
- noflags(origflags);
- curbuf = do_select(curwind, Mainbuf);
-
- #ifdef UNIX
- if (home = getenv("HOME"))
- ignore(joverc(sprint("%s/.joverc", home)));
- #endif
- ttinit(); /* Initialize terminal (after ~/.joverc) */
- #ifdef UNIX
- init_term();
- #endif
- copy_n(origflags, curbuf->b_flags, NFLAGS);
- /* All new buffers will have these flags when created. */
- RedrawDisplay(); /* Start the redisplay process */
- DoKeys(1);
- finish(0);
- }
-
- Beep()
- {
- message("");
- rbell(); /* Ring the bell (or flash) */
- errormsg = 0;
- }
-
- /*-------------------o.s. dependent----------------------------------------*/
-
-
- #ifdef UNIX
-
- /* Returns non-zero if a character waiting */
-
- charp()
- {
- if (Input)
- return 0;
- if (nchars) /* Quick check */
- return 1;
-
- else {
- #ifdef FIONREAD
- long c;
-
- if (ioctl(0, FIONREAD, (char *) &c) == -1)
- #else
- int c;
-
- if (ioctl(0, TIOCEMPTY, (char *) &c) == -1)
- #endif
- c = 0;
- return (c > 0);
- }
- }
-
-
-
- #ifndef SIGTSTP
- Suspend()
- {
- char *shell,
- *getenv();
- int pid;
-
- switch (pid = fork()) {
- case -1:
- complain("Fork failed");
-
- case 0:
- signal(SIGTERM, SIG_DFL);
- signal(SIGINT, SIG_DFL);
- execl(shell ? shell : "/bin/csh", "ed_shell", 0);
- message("Execl failed");
- _exit(1);
-
- default:
- signal(SIGQUIT, SIG_IGN);
- while (wait(0) != pid)
- ;
- signal(SIGQUIT, finish);
- }
- }
- #endif
-
- ttinit()
- {
- #ifdef TIOCSLTC
- ioctl(0, TIOCGLTC, (char *) &ls1);
- ls2 = ls1;
- ls2.t_suspc = (char) -1;
- ls2.t_dsuspc = (char) -1;
- ls2.t_rprntc = (char) -1;
- ls2.t_flushc = (char) -1;
- ls2.t_werasc = (char) -1;
- ls2.t_lnextc = (char) -1;
-
- #endif
-
- /* Change interupt and quit. */
- ioctl(0, TIOCGETC, (char *) &tc1);
- tc2 = tc1;
- tc2.t_intrc = '\035';
- tc2.t_quitc = (char) -1;
- if (OKXonXoff) {
- tc2.t_stopc = (char) -1;
- tc2.t_startc = (char) -1;
- }
- ttyset(1);
-
- /* Go into cbreak, and -echo and -CRMOD */
-
- ignorf(signal(SIGHUP, finish));
- ignorf(signal(SIGINT, finish));
- ignorf(signal(SIGQUIT, SIG_IGN));
- ignorf(signal(SIGBUS, finish));
- ignorf(signal(SIGSEGV, finish));
- ignorf(signal(SIGPIPE, finish));
- ignorf(signal(SIGTERM, SIG_IGN));
-
- }
-
- ttyset(n)
- {
- struct sgttyb tty;
-
- if (ioctl(0, TIOCGETP, (char *) &tty) == -1) {
- putstr("No terminal");
- exit(1);
- }
- if (n) {
- tty.sg_flags &= ~(ECHO | CRMOD);
- tty.sg_flags |= (RAW);
- } else {
- tty.sg_flags |= (ECHO | CRMOD);
- tty.sg_flags &= ~(RAW);
- }
- if (ioctl(0, TIOCSETN, (char *) &tty) == -1) {
- putstr("cbreak?");
- exit(1);
- }
- ioctl(0, TIOCSETC, n == 0 ? (char *) &tc1 : (char *) &tc2);
- #ifdef TIOCSLTC
- ioctl(0, TIOCSLTC, n == 0 ? (char *) &ls1 : (char *) &ls2);
- #endif
- }
-
-
- #ifndef PROFILE
- exit(status)
- {
-
- flusho();
- _exit(status);
- }
- #endif
-
-
- /* clone */
-
- #else
-
- charp()
- {
- int c;
-
- if (Input)
- return 0;
- if (nchars) /* Quick check */
- return 1;
-
- else {
- if(rawchkc())
- return(1);
- else return(0);
- }
- }
-
- /* null routine for testing. replace with exec call */
-
- Suspend()
- {
- }
-
- ttinit()
- {
- }
-
- ttyset(n)
- int n;
- {
- }
-
- conread(file,buf,size)
- unsigned char *buf;
- {
- int p;
- do {
- p = rawgetc();
- if(p == 0x0300) p = 3;
- else p &= 0x7f;
- } while (p == 0);
- *buf++ = p;
- return(1);
- }
- #endif
-
- /* end */
-